package montaltoView

import _root_.montaltoGrids._
import _root_.montaltoNeds._

/**
 * Created by IntelliJ IDEA.
 * User: Andrew Montalto
 * Date: May 2, 2010
 * Time: 12:32:36 AM
 * To change this template use File | Settings | File Templates.
 */
 

//observer design pattern 
trait NedObserver
{
  def update(grid:Grid)
  def printResults()
}

class FindMaxNedsPerTurn extends NedObserver{
  var max = 0
  var turn = 0
  def update(grid:Grid)={
     if (grid.countLivingNeds>max){
       max = grid.countLivingNeds
       turn = grid.turn
      // println("Max neds so far " + max + " on turn " + turn)
     }
  }
  def printResults()={
    println("Max neds =" + max + " on turn " + turn)
  }
}


class TotalTurns extends NedObserver{
  var  total = 0
  def update(grid:Grid)={
    this.total = grid.turn
  }
  def printResults()={
    println("Total turns =" + total)
  }
}

class OldestNed extends NedObserver{
  var oldestNed:Ned =  NedBuilder.random
  def update(grid:Grid)={
    val myIterator = GridIteratorCreator.create(grid);
    myIterator.first()
    while(myIterator.hasNext){
        myIterator.current().neds.foreach{
          n:Ned=> if(n.age > oldestNed.age){
            oldestNed = n
          }
        }
        myIterator.next()
    }

  }
  def printResults()={
    println("Oldest Ned was " + oldestNed.age + " with genetic code " + oldestNed.geneticCode)
  }
}

class MostMated extends NedObserver{
  var MostMatedNed:Ned =  NedBuilder.random
  def update(grid:Grid)={
    val myIterator = GridIteratorCreator.create(grid);
    myIterator.first()
    while(myIterator.hasNext){
        myIterator.current().neds.foreach{
          n:Ned=> if(n.mates > MostMatedNed.mates){
            MostMatedNed = n
          }
        }
        myIterator.next()
    }

  }
  def printResults()={
    println("Most mates by a ned was " + MostMatedNed.mates + " with genetic code " + MostMatedNed.geneticCode)
  }
}



